package com.flyan.game.motafx.core.util.animate;

import com.almasb.fxgl.texture.AnimationChannel;
import javafx.scene.image.Image;
import javafx.util.Duration;

/**
 * 静止动画表（不会移动的动画）
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/1/31
 */
public class StillAnimateTable {

    /**
     * 动画列表
     */
    private AnimationChannel[] table;

    private final Image image;
    private final int framesPreRow;
    private final int frameWidth;
    private final int frameHeight;
    private final Duration duration;

    public StillAnimateTable(Image image, int framesPreRow, int frameWidth, int frameHeight, Duration duration) {
        this.image = image;
        this.framesPreRow = framesPreRow;
        this.frameWidth = frameWidth;
        this.frameHeight = frameHeight;
        this.duration = duration;
        init();
    }

    public StillAnimateTable(Image image, int framesPreRow, int frameWidth, int frameHeight) {
        this(image, framesPreRow, frameWidth, frameHeight, Duration.INDEFINITE);
    }

    public StillAnimateTable(Image image, int frameWidth, int frameHeight) {
        this(image, 1, frameWidth, frameHeight, Duration.INDEFINITE);
    }

    /**
     * 初始化动画
     */
    private void init() {
        /* 动态计算数量 */
        var n = (int) (image.getHeight() / frameHeight);
        table = new AnimationChannel[n];
        for (int i = 0; i < n; i++) {
            var ac = new AnimationChannel(image, framesPreRow, frameWidth, frameHeight,
                    duration, i * framesPreRow, i * framesPreRow + (framesPreRow - 1));
            table[i] = ac;
        }
    }

    /**
     * 获取动画
     *
     * @param typeCode 类型编码
     * @return 动画
     */
    public AnimationChannel get(int typeCode) {
        if (typeCode < table.length) {
            return table[typeCode];
        }
        return null;
    }

}
